﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "TestDragEventData.h"
#include "IUserDragEventArgs.h"
#include "UiDocumentViewUtils.h"
#include "IFamily.h"
#include "IElementBasicInformation.h"
#include "IUserTransaction.h"
#include "IPointArrayInstanceInput.h"
#include "IUiView.h"
#include "IModelView.h"
#include "IInstance.h"
#include "IUiDocumentViewManager.h"
#include "ICanvas.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

/// \brief 测试用户拖放事件数据
TestDragEventData::TestDragEventData()
    :m_picturePath(L"")
    ,m_text(L"")
{
}


TestDragEventData::~TestDragEventData()
{
}

bool gcmp::TestDragEventData::IsDataValid(int  uiViewId)
{
    IUiDocumentViewManager *pDocManager = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDocManager, L"pDocManager为空",L"GDMPLab",L"2024-03-30");
    IUiView *pUiView = pDocManager->GetUiViewByRuntimeId(uiViewId);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pUiView, L"pUiView为空",L"GDMPLab",L"2024-03-30");
    IModelView *pModelView = pUiView->GetModelView();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
    const std::wstring& modelViewName = pModelView->GetName();
    if (modelViewName == L"三维")
    {
        SetText(L"三维视图构件");
        SetPicturePath(L":/images/cursor_cross.png");
    }
    else
        return false;

    
    return true;
}

void gcmp::TestDragEventData::SetPicturePath(const std::wstring & path)
{
    m_picturePath = path;
}

const std::wstring & gcmp::TestDragEventData::GetPicturePath() const
{
    return m_picturePath;
}

void gcmp::TestDragEventData::SetText(const std::wstring & tip)
{
    m_text = tip;
}

const std::wstring & gcmp::TestDragEventData::GetText()  const 
{
    return m_text;
}

void gcmp::TestDragEventData::SetFamilyName(const std::wstring & familyName)
{
    m_familtName = familyName;
}

const std::wstring & gcmp::TestDragEventData::GetFamilyName() const
{
    return m_familtName;
}

void gcmp::TestDragEventData::SetFamilyType(const std::wstring & familyType)
{
    m_familyType = familyType;
}

const std::wstring & gcmp::TestDragEventData::GetFamilyType() const
{
    return m_familyType;
}

gcmp::TestDropEventHandler::TestDropEventHandler()
{
}

gcmp::TestDropEventHandler::~TestDropEventHandler()
{
}

void gcmp::TestDropEventHandler::On(IUserDragEventArgs * pArgs)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pArgs, L"pArgs 为空",L"GDMPLab",L"2024-03-30");
    IUserDragEventData *pUserData = pArgs->GetUserDragEventData();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pUserData, L"pUserData 为空",L"GDMPLab",L"2024-03-30");
    if (TestDragEventData *pCurrentData = dynamic_cast<TestDragEventData *>(pUserData))
    {
        IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc, L"doc为空",L"GDMPLab",L"2024-03-30");

        std::wstring familyName = pCurrentData->GetFamilyName();
        std::wstring familyType = pCurrentData->GetFamilyType();

        IFamily *pFamily = IFamily::GetFamilyByName(UiDocumentViewUtils::GetCurrentDocument(), familyName);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pFamily, L"根据名字无法找到族",L"GDMPLab",L"2024-03-30");
        IElementBasicInformation* basicInformation = pFamily->GetBasicInformation();
        DBG_WARN_AND_RETURN_VOID_UNLESS(basicInformation, L"basicInformation为空",L"GDMPLab",L"2024-03-30");
        ElementId familyId = basicInformation->GetElementId();

        Vector3d pos = pArgs->GetMouseDropPosition();

        std::vector<Vector3d> pts;
        pts.emplace_back(Vector3d(pos.GetX(), pos.GetY(), pos.GetZ()));
        pts.emplace_back(Vector3d(pos.GetX()+10000, pos.GetY(), pos.GetZ()));

        int viewId = pArgs->GetCurrentUiviewId();
        IUiDocumentViewManager *pDocManager = IUiDocumentViewManager::Get();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pDocManager, L"pDocManager为空",L"GDMPLab",L"2024-03-30");
        IUiView *pUiView = pDocManager->GetUiViewByRuntimeId(viewId);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pUiView, L"pUiView为空",L"GDMPLab",L"2024-03-30");
        IModelView *pModelView = pUiView->GetModelView();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pModelView, L"pModelView 为空",L"GDMPLab",L"2024-03-30");
        ElementId planeId = pModelView->GetWorkPlaneId();

        gcmp::OwnerPtr<IUserTransaction> ut = IUserTransaction::Create(pDoc, (GBMP_TR(L"创建GBMP普通线式族实例")));
        DBG_WARN_AND_RETURN_VOID_UNLESS(ut, L"ut 为空",L"GDMPLab",L"2024-03-30");
        OwnerPtr<IPointArrayInstanceInput> opPointArrayInstanceInput = IPointArrayInstanceInput::Create(UiDocumentViewUtils::GetCurrentDocument(), planeId, familyId, familyType, pts);
        IInstance* instance = IInstance::CreateByPointArray(opPointArrayInstanceInput.get());
        DBG_WARN_AND_RETURN_VOID_UNLESS(instance, L"instance 为空",L"GDMPLab",L"2024-03-30");

        ut->Commit();
        ICanvas* pCanvas = pUiView->GetCanvas();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pCanvas, L"pCanvas 为空",L"GDMPLab",L"2024-03-30");
        pCanvas->Refresh();
    }
}
